//
//  CCircleSafeZone.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 7/3/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleSafeZone.h"

@implementation CircleSafeZone

-(id) initWithStartAngle:(float)startangle range:(float)range width:(float)width
{
    if (self = [super init])
    {
        mStartAngle = startangle;
        mRange = range;
        mWidth = width / 2;
        mAlpha = 1.0f;
        
        mSegment = mRange / SAFE_ZONE_ANGLE_PER_SEGMENT + 1;
        mVertices = (CGPoint *)calloc(mSegment * 2, sizeof(CGPoint));
        mCoordinates = (CGPoint *)calloc(mSegment * 2, sizeof(CGPoint));
        
        CCSprite *sprite = [CCSprite spriteWithFile:@"safezone.png"];
        mTexture = [sprite texture];
        
        self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTexture_uColor];

        [self.shaderProgram link];
        [self.shaderProgram updateUniforms];
    }
    return self;
}

-(void) setRadius:(float)radius
{
    float vx;
    float vy;
    float cx;
    float cy;
    float ir = radius - mWidth;
    float or = radius + mWidth;
    float a = mStartAngle;
    for (int i = 0, length = mSegment * 2; i < length; i++)
    {
        if (i % 2 == 0)
        {
            vx = ir * sinf(CC_DEGREES_TO_RADIANS(a));
            vy = ir * cosf(CC_DEGREES_TO_RADIANS(a));
        }
        else
        {
            vx = or * sinf(CC_DEGREES_TO_RADIANS(a));
            vy = or * cosf(CC_DEGREES_TO_RADIANS(a));
        }
        
        cx = ((float)i / 2) / mSegment;
        cy = (i % 2) == 0 ? 1 : 0;

        mVertices[i] = ccp(vx, vy);
        mCoordinates[i] = ccp(cx, cy);
        if (i % 2 == 1)
            a += mRange / (mSegment - 2);
    }
}

-(void) draw
{
    if (mAlpha <= 0.0) return;
    
    CC_NODE_DRAW_SETUP();
    
    float myColors[4] = {mAlpha, mAlpha, mAlpha, mAlpha};
    GLint myLoc = glGetUniformLocation(self.shaderProgram->_program , "u_color");
    glUniform4fv(myLoc, 1, myColors);
    
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords);
    ccGLBindTexture2D([mTexture name]);
    ccGLBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    
    
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, mVertices);
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, mCoordinates);
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, mSegment * 2 - 2);
	
    CC_INCREMENT_GL_DRAWS(1);
}

-(NSString *) getSpriteFrameName
{
    return FRAME_NAME_CIRCLE_SEGMENT_COLOR_RED;
}

-(void) runEffect:(EFFECT_TYPE)effect
{
    if (effect == EFFECT_APPEAR)
    {
        mAlpha = 0.0f;
        [self schedule:@selector(appear:)];
    }
    else if (effect == EFFECT_DISAPPEAR)
    {
        mAlpha = 1.0f;
        [self schedule:@selector(disappear:)];
    }
}

-(void) appear:(ccTime)delta
{
    mAlpha += 1.0 / TIME_DISAPPEAR * delta;
    if (mAlpha >= 1.0)
    {
        mAlpha = 1.0;
        [self unschedule:_cmd];
    }
}

-(void) disappear:(ccTime)delta
{
    mAlpha -= 1.0 / TIME_APPEAR * delta;
    if (mAlpha <= 0)
    {
        mAlpha = 0;
        [self unschedule:_cmd];
    }
}

@end
